<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>

<input value="Hello, World">
<script>
let input = document.querySelector('input');

// Note: Do not upstream this to web platform tests.
// - beforecopy / beforecut events are not standardized.
// - document.execCommand() is not guaranteed to generate events on
//   non-Blink engines because it is not clearly specified in the standards.
// - For Blink, document.execCommand() only guaranteed to generate clipboard
//   events on user gestures or layout tests.

async_test((test) => {
  assert_true(window.testRunner instanceof Object,
	      'This test must run where window.testRunner is available.');
  input.value = "Hello, World";
  input.selectionStart = 0;
  input.selectionEnd = 10;
  let beforecopy_done = false;
  document.addEventListener('beforecopy', test.step_func((e) => {
    assert_true(e.isTrusted, 'beforecopy event should be trusted');
    assert_true(e.composed, 'beforecopy event should be composed');
    beforecopy_done = true;
  }));
  document.addEventListener('copy', test.step_func_done((e) => {
    assert_true(beforecopy_done);
    assert_true(e.isTrusted, 'copy event should be trusted');
    assert_true(e.composed, 'copy event should be composed');
  }));
  document.execCommand('copy');
}, 'beforecopy/copy event is composed.');

async_test((test) => {
  assert_true(window.testRunner instanceof Object,
	      'This test must run where window.testRunner is available.');
  input.value = "Hello, World";
  input.selectionStart = 0;
  input.selectionEnd = 10;
  let beforecut_done = false;
  document.addEventListener('beforecut', test.step_func((e) => {
    assert_true(e.isTrusted, 'beforecut event should be trusted');
    assert_true(e.composed, 'beforecut event should be composed');
    beforecut_done = true;
  }));
  document.addEventListener('cut', test.step_func_done((e) => {
    assert_true(beforecut_done);
    assert_true(e.isTrusted, 'cut event should be trusted');
    assert_true(e.composed, 'cut event should be composed');
  }));
  document.execCommand('cut');
}, 'beforecut/cut event is composed.');

async_test((test) => {
  assert_true(window.testRunner instanceof Object,
	      'This test must run where window.testRunner is available.');
  input.value = "";
  input.selectionStart = 0;
  input.selectionEnd = 0;
  // Blink doesn't implement beforepaste event.
  document.addEventListener('paste', test.step_func_done((e) => {
    assert_true(e.isTrusted, 'paste event should be trusted');
    assert_true(e.composed, 'paste event should be composed');
  }));
  document.execCommand('paste');
}, 'paste event is composed.');
</script>
